import {observable as o, computed as c, action as a} from 'mobx';
import _ from 'lodash';
import axios from 'core/axios';
import RootStore from 'core/Store.js';
import request from 'core/request.js';
import Im from 'immutable';
import {browserHistory} from 'react-router';

class Store extends RootStore {
    constructor(args) {
        super(args);
    }

    @o visible = false;
    @o agentUserId = '';
    @o agentUserName = '';
    @o agentUserPhone = '';
    @o recordState = 1;

    @o registerList = [];
    @o registerTotal = 0;
    @o registerRequest = false;
    @o registerPage = 1;

    //注册记录检索
    @o registerIdentity = '';
    @o registerStartTime = '';
    @o registerEndTime = '';

    @o purchaseList = [];
    @o purchaseTotal = 0;
    @o purchaseRequest = false;
    @o purchasePage = 1;

    //购买记录检索
    @o purchaseIdentity = '';
    @o purchaseVersion = '';
    @o purchasePayWay = '';
    @o purchaseStartTime = '';
    @o purchaseEndTime = '';

    will() {

    }

    closeDialog() {
        this.visible = false;
        this.setToDefault();
    }

    setToDefault() {
        this.agentUserId = '';
        this.agentUserName = '';
        this.agentUserPhone = '';
        this.recordState = 1;

        this.registerList = [];
        this.registerTotal = 0;
        this.registerRequest = false;
        this.registerPage = 1;

        this.purchaseList = [];
        this.purchaseTotal = 0;
        this.purchaseRequest = false;
        this.purchasePage = 1;
    }


    // 检索value变化
    startTimeChange(e) {
        if (this.recordState === 1) {
            this.registerStartTime = e._d && e._d.getTime();
        } else {
            this.purchaseStartTime = e._d && e._d.getTime();
        }
        this.getList(true, 1);
    }

    endTimeChange(e) {
        if (this.recordState === 1) {
            this.registerEndTime = e._d && e._d.getTime();
        } else {
            this.purchaseEndTime = e._d && e._d.getTime();
        }
        this.getList(true, 1);
    }

    identityChange(v) {
        if (this.recordState === 1) {
            this.registerIdentity = v;
        } else {
            this.purchaseIdentity = v;
        }
        this.getList(true, 1);
    }

    versionChange(v) {
        this.purchaseVersion = v;
        this.getList(true, 1);
    }

    payWayChange(v) {
        this.purchasePayWay = v;
        this.getList(true, 1);
    }

    changeState(state) {
        this.recordState = state;
        if ((this.recordState === 1 && this.registerList.length === 0) ||
            (this.recordState === 2 && this.purchaseList.length === 0)) {
            this.getList(true, 1);
        }
    }

    paginationChange(current) {
        this.getList(true, current);
    }

    getRegisterList(loading, current) {
        loading && this.stores.Frame.showLoading();
        this.registerPage = !!current ? current : this.registerPage;
        request({
            url: $$.api('agentsRegisterRecord'),
            method: 'post',
            data: {
                userId: this.agentUserId,
                pageSize: 20,
                pageNumber: this.registerPage,
                beginTime: this.registerStartTime || null,
                endTime: this.registerEndTime || null,
                label: this.registerIdentity || null,
            }
        }).then(res => {
            this.stores.Frame.hideLoading();
            this.registerList = res.data.list;
            this.registerTotal = res.data.total;
            this.registerRequest = true;
        }).catch(err => {
            this.stores.Frame.hideLoading();
            this.stores.Message.showMessage(2, err.message);
        });
    }

    exportRegisterList() {
        this.stores.Frame.showLoading();
        request({
            url: $$.api('agentsRegisterRecord'),
            method: 'post',
            data: {
                userId: this.agentUserId,
                beginTime: this.registerStartTime || null,
                endTime: this.registerEndTime || null,
                label: this.registerIdentity || null,
                export: 'string'
            }
        }).then(res => {
            this.stores.Frame.hideLoading();
            $$.utils.customNameToDownload('https://' + res.data.url, `${this.agentUserPhone}_${this.agentUserName}_注册记录`);
        }).catch(err => {
            this.stores.Frame.hideLoading();
            this.stores.Message.showMessage(2, err.message);
        });
    }

    getPurchaseList(loading, current) {
        loading && this.stores.Frame.showLoading();
        this.purchasePage = !!current ? current : this.purchasePage;
        request({
            url: $$.api('agentsPurchaseRecord'),
            method: 'post',
            data: {
                userId: this.agentUserId,
                pageSize: 20,
                pageNumber: this.purchasePage,
                beginTime: this.purchaseStartTime || null,
                endTime: this.purchaseEndTime || null,
                label: this.purchaseIdentity || null,
                buyVersion: this.purchaseVersion || null,
                payWay: this.purchasePayWay || null,
            }
        }).then(res => {
            this.purchaseList = res.data.list;
            this.purchaseTotal = res.data.total;
            this.stores.Frame.hideLoading();
            this.purchaseRequest = true;
        }).catch(err => {
            this.stores.Frame.hideLoading();
            this.stores.Message.showMessage(2, err.message);
        });
    }

    exportPurchaseList() {
        this.stores.Frame.showLoading();
        request({
            url: $$.api('agentsPurchaseRecord'),
            method: 'post',
            data: {
                userId: this.agentUserId,
                beginTime: this.purchaseStartTime || null,
                endTime: this.purchaseEndTime || null,
                label: this.purchaseIdentity || null,
                buyVersion: this.purchaseVersion || null,
                payWay: this.purchasePayWay || null,
                export: 'string'
            }
        }).then(res => {
            this.stores.Frame.hideLoading();
            $$.utils.customNameToDownload('https://' + res.data.url, `${this.agentUserPhone}_${this.agentUserName}_购买记录`);
        }).catch(err => {
            this.stores.Frame.hideLoading();
            this.stores.Message.showMessage(2, err.message);
        });
    }

    exportData() {
        if (this.recordState === 1) {
            this.exportRegisterList();
        } else {
            this.exportPurchaseList();
        }
    }


    getList(loading, current) {
        if (this.recordState === 1) {
            if (!!this.registerEndTime && !!this.registerStartTime && this.registerEndTime < this.registerStartTime) {
                this.stores.Message.showMessage(3, '结束时间不能小于开始时间');
                return;
            }
            this.getRegisterList(loading, current);
        } else {
            if (!!this.purchaseEndTime && !!this.purchaseStartTime && this.purchaseEndTime < this.purchaseStartTime) {
                this.stores.Message.showMessage(3, '结束时间不能小于开始时间');
                return;
            }
            this.getPurchaseList(loading, current);
        }
    }


    deleteClick(item) {
        this.stores.ConfirmDialog.fire('confirmDialog:show', {
            tips: '是否要删除该条注册记录?',
            func: () => {
                this.stores.Frame.showLoading();
                request({
                    url: $$.api('agentsDeleteUser') + item.id,
                    method: 'delete'
                }).then(res => {
                    this.getList();
                    this.stores.Message.showMessage(1, res.message);
                }).catch(err => {
                    this.stores.Frame.hideLoading();
                    this.stores.Message.showMessage(2, err.message);
                });
            }
        })
    }

    mount() {
        this.on('agentsDetail:show', (data) => {
            this.visible = true;
            if (data) {
                this.agentUserId = data.id;
                this.agentUserName = data.name;
                this.agentUserPhone = data.phone;
                this.getList(false, 1);
            }
        });
    }

}

export default Store;